#!/usr/bin/python

import pylab
import networkx as nx
import argparse

from utils import *
from gleiss import *

cmd_parser = argparse.ArgumentParser()
cmd_parser.add_argument("FILE", help="Graph adjacency matrix file",
                type=argparse.FileType('r'))
cmd_parser.add_argument("-d", "--draw", help="Draw graph with circuit basis marked red",
                action="store_true")
cmd_parser.add_argument("-z", "--mark_z", help="Additionally print z vertice used for x-y-z path (x,y are at the beginning of path)",
                action="store_true")
cmd_parser.add_argument("-m", "--mod", help="Use RGreedy algorithm modifications invented by program authors",
                action="store_true")
cmd_parser.add_argument("-p", "--debug", help="Print additional debug information, showing the algorithm steps",
				action="store_true")
cmd_parser.add_argument("-i", "--info", help="Print additional graph information",
                action="store_true")


args = cmd_parser.parse_args()

#graph = graph_from_adjmat_file('../data/test.in')
graph = file2graph(args.FILE)

def print_summary(circuits):
	print circuits
	weight_sum = 0
	for circuit in circuits:
		weight_sum = weight_sum + circuit.weight
	print "Size: %d, weights sum: %f" % (len(circuits), weight_sum)


ac = get_circuits(graph, args.mark_z)
print "arc-short circuits and bidirectional edges:"
print_summary(ac)
print ""

(min_base, relevant) = r_greedy(ac, args.mod, args.debug, graph)
print "min circuit base:"
print_summary(min_base)
print ""

print "|.|-relevant circuits:"
print_summary(relevant)

if args.info:
        
        print ""

        print "Additional input graph information:"

        if (nx.is_strongly_connected(graph)):
                print "* Graph is strongly connected,"  
        else:
                print "* Graph is not strongly connected,"

        if (nx.is_weakly_connected(graph)):
                print "* Graph is weakly connected,"  
        else:
                print "* Graph is not weakly connected,"


if args.draw:
        pos = nx.circular_layout(graph)
        normal_edges = []
        base_edges = []
        
        if not min_base:
                normal_edges = graph.edges()
        else:
                for graph_edge in graph.edges():
                        for circuit in min_base:
                                if graph_edge in circuit._create_edge_list():
                                        base_edges.append(graph_edge)
                                else:
                                        normal_edges.append(graph_edge)

        nx.draw_networkx_edges(graph, pos, base_edges, edge_color='r', width=1.0)
        nx.draw_networkx_edges(graph, pos, normal_edges, edge_color='b', width=0.1)
        nx.draw_networkx_nodes(graph, pos, graph.nodes(), node_color='c')
        nx.draw_networkx_labels(graph, pos)
        pylab.show()


exit(0)
# example showing independence for strcon8.in case
set_weights(graph)
paths = [[1,2,3],[0,1,2],[2,3,4],[4,5,6],[0,6,7],[3,1,7,5]]
cir = []
for path in paths:
	cc = Circuit(path, graph)
	cir.append(cc)

print cir
edges = graph.edges()
edges.sort()
print("")
res = independent(cir, True, edges)
print("Independent: "),
print res
